<template>
  <div>
    <el-dialog :title="title" :visible.sync="is_show" @closed="closed" center width="650px">
      <el-form label-position="left" ref="form" :rules="rules" label-width="100px" :model="form">
        <el-form-item label="修改类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择修改类型">
            <el-option label="奖励" value="reward"></el-option>
            <el-option label="惩戒" value="publish"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="form.type == 'reward' ? '奖励理由' : '惩戒理由'" prop="reason">
          <el-input v-model="form.reason"></el-input>
        </el-form-item>
        <el-form-item :label="form.type == 'reward' ? '奖励金额' : '惩戒金额'" prop="reward">
          <el-input v-model="form.reward" type="number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="is_show = false">取 消</el-button>
        <el-button type="primary" @click="submit" v-no-more-click:3000>确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      is_show: false,
      type: "reward",
      form: {
        reward: "",
        reason: ""
      },
      agent_id: null,
      title: "修改钱包",
      rules: {
        type: [{ required: true, message: "请选择修改类型", trigger: "blur" }],
        reason: [{ required: true, message: "请输入理由", trigger: "blur" }],
        reward: [{ required: true, message: "请输入金额", trigger: "blur" }]
      }
    };
  },
  props: {
    agentOptions: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    closed() {
      // Object.assign(this.$data, this.$options.data());
      this.$refs["form"].resetFields();
    },
    show(data) {
      this.agent_id = data.id
      this.is_show = true;
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.type == "reward") {
            this.rewardPost();
          } else {
            this.publishPost();
          }
        }
      });
    },
    publishPost() {
      let params = {
        agent_id: this.agent_id,
        deduction_price: this.form.reward,
        name: this.form.reason
      };
      this.$axios.post("/api/deduction/agent_deduction", params).then(res => {
        if (res.data.code == 200) {
          this.$message({
            type: "success",
            message: "操作成功"
          });
          this.$emit("refresh");
          this.is_show = false;
        } else {
          this.$message({
            type: "error",
            message: res.data.message
          });
        }
      });
    },
    rewardPost() {
      let params = {
        agent_id: this.agent_id,
        reason: this.form.reason,
        reward: this.form.reward
      };
      this.$axios
        .post("/api/rewardRecord/agent_reward_record", params)
        .then(res => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "操作成功"
            });
            this.$emit("refresh");
            this.is_show = false;
          } else {
            this.$message({
              type: "error",
              message: res.data.message
            });
          }
        });
    }
  }
};
</script>

<style lang='less' type='text/css'>
.el-select {
  width: 100%;
}
</style>
